package org.wzq.android_api;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;

/**
 * need root
 */
public class Act_Install extends Activity {
    Context mctx;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_install);
        mctx = this;

        findViewById(R.id.btn_install_normal).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File sdcard = Environment.getExternalStorageDirectory();
                File apkFile = new File(sdcard, "dev/Browser.apk");
                if (!apkFile.exists()) {
                    Toast.makeText(mctx, "apk文件不存在", Toast.LENGTH_SHORT).show();
                    return;
                }
                install_normal(apkFile);
            }
        });

        findViewById(R.id.btn_install_silently).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(mctx, "未支持", Toast.LENGTH_SHORT).show();
                File sdcard = Environment.getExternalStorageDirectory();
                File apkFile = new File(sdcard, "dev/Browser.apk");
                if (!apkFile.exists()) {
                    Toast.makeText(mctx, "apk文件不存在", Toast.LENGTH_SHORT).show();
                    return;
                }

                boolean result = false;
                //install_silently_test1(apkFile.getAbsolutePath());
                //install_silently_test2(apkFile.getAbsolutePath());
                result = install_silently_test3(apkFile.getAbsolutePath());
                if (result) {
                    Toast.makeText(mctx, "安装成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(mctx, "安装失败", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    /**
     * fail
     */
    private static void install_silently_test1(String fileName) {
        try {
            // 没有权限
            // Runtime.getRuntime().exec("pm install /sdcard/dev/Browser.apk").waitFor();

            // 加上su 没有报错，但是也未安装
            Runtime.getRuntime().exec("su pm install " + fileName).waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * fail
     */
    public static void install_silently_test2(String filename) {
        File file = new File(filename);
        if (file.exists()) {
            try {
                String command;
                command = "adb install " + filename;
                // command = "adb install -r " + filename;
                System.out.println("command = " + command);
                Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", command});
                proc.waitFor();
                System.out.println("install succ");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * OK 4.0.x root权限
     */
    private static boolean install_silently_test3(String filename) {
        File file = new File(filename);
        if (file.exists()) {
            try {
                final String command = "pm install -r " + file.getAbsolutePath();
                Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", command});
                proc.waitFor();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    private void install_normal(File apkFile) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
        startActivity(intent);
    }
}
